package com.telecomyt.jwportal.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 坐标转换工具类
 * WGS84坐标系：即地球坐标系，国际上通用的坐标系。Earth          (GPS坐标系)
 * GCJ02坐标系：即火星坐标系，WGS84坐标系经加密后的坐标系。Mars  (谷歌地图(中国)、高德地图、腾讯地图)
 * BD09坐标系：即百度坐标系，GCJ02坐标系经加密后的坐标系。  Bd09 (百度地图)
 * 搜狗坐标系、图吧坐标系等，估计也是在GCJ02基础上加密而成的。 （该类地图使用较少）
 */
public class MapUtils {
    /**
     * 元周率
     */
    private static final double PI = Math.PI;
    /**
     * 卫星椭球坐标投影到平面地图坐标系的投影因子
     */
    private static final double AXIS = 6378245.0;
    /**
     * 椭球的偏心率 (a^2 - b^2) / a^2
     */
    private static final double OFFSET = 0.00669342162296594323;
    private static final double X_PI = PI * 3000.0 / 180.0;

    /**
     * 地球半径,单位 m
     */
    private static final double EARTH_RADIUS = 6378137.0;

    /**
     * GCJ-02=>BD09 火星坐标系=>百度坐标系
     */
    public static double[] gcj2BD09(double glat, double glon) {
        double x = glon;
        double y = glat;
        double[] latlon = new double[2];
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
        latlon[0] = z * Math.sin(theta) + 0.006;
        latlon[1] = z * Math.cos(theta) + 0.0065;
        return latlon;
    }

    /**
     * BD09=>GCJ-02 百度坐标系=>火星坐标系
     */
    public static double[] bd092GCJ(double glat, double glon) {
        double x = glon - 0.0065;
        double y = glat - 0.006;
        double[] latlon = new double[2];
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
        latlon[0] = z * Math.sin(theta);
        latlon[1] = z * Math.cos(theta);
        return latlon;
    }

    /**
     * BD09=>WGS84 百度坐标系=>地球坐标系
     */
    public static double[] bd092WGS(double glat, double glon) {
        double[] latlon = bd092GCJ(glat, glon);
        return gcj2WGSExactly(latlon[0], latlon[1]);
    }

    /**
     * WGS84=>BD09   地球坐标系=>百度坐标系
     */
    public static double[] wgs2BD09(double wgLat, double wgLng) {
        double[] latlon = wgs2GCJ(wgLat, wgLng);
        return gcj2BD09(latlon[0], latlon[1]);
    }

    /**
     * WGS84=>GCJ02   地球坐标系=>火星坐标系
     */
    public static double[] wgs2GCJ(double wgLat, double wgLng) {
        double[] latlon = new double[2];
        if (outOfChina(wgLat, wgLng)) {
            latlon[0] = wgLat;
            latlon[1] = wgLng;
            return latlon;
        }
        double[] deltaD = delta(wgLat, wgLng);
        latlon[0] = wgLat + deltaD[0];
        latlon[1] = wgLng + deltaD[1];
        return latlon;
    }

    /**
     * GCJ02=>WGS84   火星坐标系=>地球坐标系(粗略)
     */
    public static double[] gcj2WGS(double glat, double glon) {
        double[] latlon = new double[2];
        if (outOfChina(glat, glon)) {
            latlon[0] = glat;
            latlon[1] = glon;
            return latlon;
        }
        double[] deltaD = delta(glat, glon);
        latlon[0] = glat - deltaD[0];
        latlon[1] = glon - deltaD[1];
        return latlon;
    }

    /**
     * GCJ02=>WGS84   火星坐标系=>地球坐标系（精确）
     */
    public static double[] gcj2WGSExactly(double gcjLat, double gcjLng) {
        double initDelta = 0.01;
        double threshold = 0.000000001;
        double dLat = initDelta, dLng = initDelta;
        double mLat = gcjLat - dLat, mLng = gcjLng - dLng;
        double pLat = gcjLat + dLat, pLng = gcjLng + dLng;
        double wgsLat, wgsLng, i = 0;
        while (true) {
            wgsLat = (mLat + pLat) / 2;
            wgsLng = (mLng + pLng) / 2;
            double[] tmp = wgs2GCJ(wgsLat, wgsLng);
            dLat = tmp[0] - gcjLat;
            dLng = tmp[1] - gcjLng;
            if ((Math.abs(dLat) < threshold) && (Math.abs(dLng) < threshold)) {
                break;
            }

            if (dLat > 0) {
                pLat = wgsLat;
            } else {
                mLat = wgsLat;
            }
            if (dLng > 0) {
                pLng = wgsLng;
            } else {
                mLng = wgsLng;
            }

            if (++i > 10000) {
                break;
            }
        }
        double[] latlon = new double[2];
        latlon[0] = wgsLat;
        latlon[1] = wgsLng;
        return latlon;
    }

    /**
     * 根据经纬度，计算两点间的距离
     *
     * @param longitude1 第一个点的经度
     * @param latitude1  第一个点的纬度
     * @param longitude2 第二个点的经度
     * @param latitude2  第二个点的纬度
     * @return 返回距离 单位米
     */
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 纬度
        double lat1 = Math.toRadians(latitude1);
        double lat2 = Math.toRadians(latitude2);
        // 经度
        double lng1 = Math.toRadians(longitude1);
        double lng2 = Math.toRadians(longitude2);
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lng1 - lng2;
        // 计算两点距离的公式
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘地球半径, 返回单位: 米
        s =  s * EARTH_RADIUS;
        return s;
    }

    private static double[] delta(double wgLat, double wgLng) {
        double[] latlng = new double[2];
        double dLat = transformLat(wgLng - 105.0, wgLat - 35.0);
        double dLng = transformLng(wgLng - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - OFFSET * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((AXIS * (1 - OFFSET)) / (magic * sqrtMagic) * PI);
        dLng = (dLng * 180.0) / (AXIS / sqrtMagic * Math.cos(radLat) * PI);
        latlng[0] = dLat;
        latlng[1] = dLng;
        return latlng;
    }

    private static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347) {
            return true;
        }
        if (lat < 0.8293 || lat > 55.8271) {
            return true;
        }
        return false;
    }

    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLng(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 精确计算两个除数的结果
     * @param d1    除数
     * @param d2    被除数
     * @param digit 精度
     * @return: double
     */
    public static double calDvi(double d1, double d2, int digit) {
        if (d2 == 0) {
            return 0;
        }
        BigDecimal bi1 = BigDecimal.valueOf(d1);
        BigDecimal bi2 = BigDecimal.valueOf(d2);
        return bi1.divide(bi2, digit, RoundingMode.HALF_UP).doubleValue();
    }

}
